﻿using System;

namespace xlstool
{
    using NPOI.HSSF.UserModel;
    using NPOI.SS.UserModel;

    public class Excel2003Workbook
    {
        internal HSSFWorkbook wb { get; set; }
        public Excel2003Workbook() {
            wb = new HSSFWorkbook();
        }
        public Excel2003Workbook(Stream xls_stream) {
            wb = new HSSFWorkbook(xls_stream);
        }

        public IEnumerable<string> LoopSheetNames()
        {
            for (int sheetIdx = 0; sheetIdx < wb.NumberOfSheets; sheetIdx++) yield return wb.GetSheetName(sheetIdx);
        }
        public IEnumerable<Excel2003Sheet> LoopSheet()
        {
            for (int sheetIdx = 0; sheetIdx < wb.NumberOfSheets; sheetIdx++)
            {
                var sheet = wb.GetSheetAt(sheetIdx);
                if (sheet == null) continue;
                yield return new Excel2003Sheet(sheet, this);
            }
        }
        public Excel2003Sheet ToSheet(string sheetName)
        {
            var sheet = wb.GetSheet(sheetName);
            if (sheet == null)
            {
                sheet = wb.CreateSheet(sheetName);
            }
            return new Excel2003Sheet(sheet, this);
        }
        internal int read_sheetno(string sheetname)
        {
            for (int sheetIdx = 0; sheetIdx < wb.NumberOfSheets; sheetIdx++) if (wb.GetSheetName(sheetIdx).Equals(sheetname, StringComparison.OrdinalIgnoreCase)) return sheetIdx;
            return -1;
        }
        internal ICellStyle CreateStyle()
        {
            return this.wb.CreateCellStyle();
        }
        internal IFont CreateFont()
        {
            return this.wb.CreateFont();
        }
        internal IFont GetFont(short index) { return this.wb.GetFontAt(index); }
        internal ICellStyle GetStyle(short index) { return this.wb.GetCellStyleAt(index); }
    }
}
